/// Author:				    Joe Audette
/// Created:			    2006-09-01
///	Last Modified:		    2010-05-19 by Joe Davis of i7MEDIA
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.	

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;

namespace mojoPortal.Web.UI
{
    /// <summary>
    /// This control was once used in the .NET 1.1 days inside the <title></title> of a page
    /// currently it can be used in layout.master if you want to show the page name or override title in the page.
    /// </summary>
    public class PageTitle : WebControl
    {
        public Literal Title = new Literal();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            // Don't use this on non cms pages, there is always a "CurrentPage" even when we are on non cms pages like sitemap
            // in that case it will be the home page
            if (!(Page is CmsPage)) { return; }
            
            PageSettings currentPage = CacheHelper.GetCurrentPage();
            if (currentPage == null) { return; }

            

            if (currentPage.PageTitle.Length > 0)
            {
                Title.Text = Context.Server.HtmlEncode(currentPage.PageTitle);
            }
            else
            {
                Title.Text = Context.Server.HtmlEncode(currentPage.PageName);
            }

        }

        

        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current == null)
            {
               
                writer.Write("[" + this.ID + "]");
            }
            else
            {
                Title.RenderControl(writer);
            }
        }

        
    }
}
